$(function () {

	function _operation_field_formatter(cellValue, options, rowData) {
		var mode = 'modify';
		var buttons = [];
		var id = rowData['id'];
		buttons.push('<div class="grid-operation-button">')

		if (mode == 'view') {
			buttons.push(str_format('<a class="label label-info" target="_blank" href="info.html?id={0}">查看</a>', rowData['id']));
		}
		if (mode == 'modify') {
			buttons.push(str_format('<a class="label label-info" onclick="T.vm.update(' + id + ',event)" href="javascript:;">修改</a>', rowData['id']));
		}
		buttons.push('</div>');
		return buttons.join("&nbsp;")
	}

	$("#jqGrid").jqGrid({
		url: baseURL + 'app/gift/list',
		datatype: "json",
		colModel: [
			{label: 'id', name: 'id', index: 'id', width: 50, key: true},
			{
				label: '编码',
				name: 'code',
				index: 'code',
				width: 80,
				formatter: get_grid_column_formatter('app/gift/code')
			},
			{
				label: '礼物类型',
				name: 'type',
				index: 'type',
				width: 80,
				formatter: get_grid_column_formatter('app/gift/type')
			},
			{
				label: '礼物名称',
				name: 'name',
				index: 'name',
				width: 80,
				formatter: get_grid_column_formatter('app/gift/name')
			},
			{
				label: '礼物资源',
				name: 'picUrl',
				index: 'pic_url',
				width: 80,
				formatter: get_grid_column_formatter('app/gift/picUrl')
			},
			{
				label: '礼物价格',
				name: 'price',
				index: 'price',
				width: 80,
				formatter: get_grid_column_formatter('app/gift/price')
			},
			{
				label: '排序',
				name: 'displayOrder',
				index: 'display_order',
				width: 80,
				formatter: get_grid_column_formatter('app/gift/displayOrder')
			},
			{
				label: '添加时间',
				name: 'adddate',
				index: 'adddate',
				width: 80,
				formatter: get_grid_column_formatter('app/gift/adddate')
			}
			, {
				label: '操作',
				name: '_operation_field',
				index: '_operation_field',
				width: 80,
				formatter: _operation_field_formatter
			}
		],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList: [10, 30, 50],
		rownumbers: false,
		rownumWidth: 25,
		autowidth: true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader: {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames: {
			page: "page",
			rows: "limit",
			order: "order"
		},
		gridComplete: function () {
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
		}
	});
});

var vm  = T.vm = new Vue({
	el: '#webpage',
	data: {
		appearance: {
			showList: true,
			showControllBar: true,
			showSearchForm: false
		},
		config : {
			groups:{},
			types : {},
			category : [],
			mark : [],
		},
		q: {
			_keyword: ''
		},
		title: null,
		gift: {

		}
	},
	watch: {
		'appearance.showList': function (value, value2) {
			if (value) {
				vm.$refs.uploader.$emit('refresh');
			}
		}
	},
	mounted : function (){
		var that = this;
		apiget('sys/config/get?name=gift.config').then(function (res){
			var data = res.data.config;
			console.log(data);
			if(data){
				that.config.groups = data['groups'];
				that.config.types = data.types;
				that.config.category = data.category;
				that.config.mark = data.mark;
				console.log(that.config)
			}
		})
	},
	methods: {
		uploadResonse: function (res) {
			if (res.code > 0) {
				this.gift.picUrl = res.url;
			} else {
				alert(res.msg);
			}
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.appearance.showList = false;
			vm.title = "新增";
			vm.gift = $.extend({},{
				type : 'pic',
				picUrl: '',
				groupName : '',
				category : 'simple',
				mark : 'simple'
			});
		},
		update: function (targetId, event) {
			var id = targetId || getSelectedRow();
			if (id == null) {
				return;
			}
			vm.appearance.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.gift.id == null ? "app/gift/save" : "app/gift/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.gift),
				success: function (r) {
					if (r.code > 0) {
						alert('操作成功', function (index) {
							vm.reload();
						});
					} else {
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if (ids == null) {
				return;
			}

			confirm('确定要删除选中的记录？', function () {
				$.ajax({
					type: "POST",
					url: baseURL + "app/gift/delete",
					contentType: "application/json",
					data: JSON.stringify(ids),
					success: function (r) {
						if (r.code > 0) {
							alert('操作成功', function (index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function (id) {
			$.get(baseURL + "app/gift/info/" + id, function (r) {
				vm.gift = r.gift;
			});
		},
		reload: function (event) {
			vm.appearance.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData: {'_keyword': vm.q._keyword},
				page: page
			}).trigger("reloadGrid");
		}
	}
});